package com.huixue.cn.ztej.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.util.StringUtil;
import com.huixue.cn.ztej.bean.Results;
import com.huixue.cn.ztej.bean.TOrderInfo;
import com.huixue.cn.ztej.bean.TQuestionsInfo;
import com.huixue.cn.ztej.bean.TTudentInfo;
import com.huixue.cn.ztej.bean.User;
import com.huixue.cn.ztej.persistence.Domain;
import com.huixue.cn.ztej.service.TQuestionsService;
import com.huixue.cn.ztej.service.TTudentService;
import com.huixue.cn.ztej.utils.SystemContents.API_RETURN_STATUS;
import com.huixue.cn.ztej.utils.framwork.JacksonUtil;
import com.huixue.cn.ztej.utils.framwork.PageParameter;

@Domain(TQuestionsInfo.class)
@Controller
@RequestMapping(value = "/sys/question")
public class TQuestionsController extends BaseController {
	@Autowired(required = true)
	private TTudentService sTudentService;
	@Autowired(required = true)
	private TQuestionsService tquestionsService;
	
	@RequestMapping(value = "/ques_list")
	public String list(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = PageParameter.DEFAULT_PAGE_SIZE+"") Integer rows,
			TQuestionsInfo searchQuesForm,Model model,HttpServletRequest request) 
	{
		Map<String,Object> searchParams = new HashMap<String,Object>();	
		if(searchQuesForm.getSubjectId()!=null){
			searchParams.put("subjectId", searchQuesForm.getSubjectId());
		}
		
		if(searchQuesForm.getSchoolId()!=null){
			searchParams.put("schoolId", searchQuesForm.getSchoolId());
		}
		
		if(searchQuesForm.getGradeId()!=null){
			searchParams.put("gradeId", searchQuesForm.getGradeId());
		}
		
		if(searchQuesForm.getStatus()!=null){
			searchParams.put("status", searchQuesForm.getStatus());
		}
		
		Results results = tquestionsService.searchByPage(searchParams,page,rows);
		model.addAttribute("res", results);
		model.addAttribute("searchQuesForm", searchQuesForm);
		return "/questions/question_list";
	}
	
	/**
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/toAdd")
	public String toAdd(Model model) {
		return "/questions/add";
	}
	
	/**
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/toUpdate/{id}")
	public String toUpate(@PathVariable ("id")Integer id,Model model,HttpServletRequest request) {
		TQuestionsInfo info = tquestionsService.getInfo(id);
		model.addAttribute("question", info);
		return "/questions/question_update";
	}
	
	/**
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/beforeExplanation/{id}")
	public String beforeExplanation(@PathVariable ("id")Integer id,Model model,HttpServletRequest request) {
		TQuestionsInfo info = tquestionsService.getInfo(id);
		model.addAttribute("roleRes", info);
		return "/questions/explanation";
	}
	
	
	/**
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/update",produces="application/json;charset=UTF-8")
	public Results udpate(@RequestBody TQuestionsInfo  info,Model model,HttpServletRequest request) {
		tquestionsService.update(info);
		Results results = new Results(API_RETURN_STATUS.NORMAL.value(), "修改成功！");
		return results;
	}
	
	/**
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/explanation",produces="application/json;charset=UTF-8",method=RequestMethod.POST)
	public Results explanation(@RequestBody TQuestionsInfo  info,Model model,HttpServletRequest request) {
		tquestionsService.explanation(info);
		Results results = new Results(API_RETURN_STATUS.NORMAL.value(), "修改成功！");
		return results;
	}

	@ResponseBody
	@RequestMapping(value = "/save",produces="application/json;charset=UTF-8",method=RequestMethod.POST)
	public Results add(@RequestBody TQuestionsInfo info,HttpServletRequest request) {
		tquestionsService.save(info);
		Results results = new Results(API_RETURN_STATUS.NORMAL.value(), "保存成功！");
		return results;
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/delete/{qeustionId}",produces="application/json;charset=UTF-8",method=RequestMethod.DELETE)
	public Results delete(@PathVariable(value="qeustionId") String qeustionId ,HttpServletRequest request) {
		try{
			tquestionsService.delete(Integer.valueOf(qeustionId));
			Results results = new Results(API_RETURN_STATUS.NORMAL.value(), "删除成功！");
			logger.debug( new JacksonUtil().getJson(results));
			return results;
		}catch(Exception e){
			Results results = new Results(API_RETURN_STATUS.SERVER_INTERNAL_ERROR.value(), "删除失败！");
			logger.debug( new JacksonUtil().getJson(results));
			return results;
		}		
	}
	
	@ResponseBody
	@RequestMapping(value = "/reslove/{qeustionId}",produces="application/json;charset=UTF-8")
	public Results reslove(@PathVariable(value="qeustionId") String qeustionId ,HttpServletRequest request) {
		try{
			TQuestionsInfo question = tquestionsService.getInfo(Integer.valueOf(qeustionId));
			question.setStatus(2);
			tquestionsService.update(question);
			Results results = new Results(API_RETURN_STATUS.NORMAL.value(), "更新成功！");
			logger.debug( new JacksonUtil().getJson(results));
			return results;
		}catch(Exception e){
			Results results = new Results(API_RETURN_STATUS.SERVER_INTERNAL_ERROR.value(), "更新失败！");
			logger.debug( new JacksonUtil().getJson(results));
			return results;
		}		
	}
	
}
